package com.industrial.premu.service.impl;

import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.premu.controller.cockpit.SsDetailDto;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.mapper.CockpitMapper;
import com.industrial.premu.service.ICockpitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CockpitServiceImpl implements ICockpitService {

    private CockpitMapper cockpitMapper;

    public CockpitServiceImpl(CockpitMapper cockpitMapper) {
        this.cockpitMapper = cockpitMapper;
    }

    /**
     * 查询年度的研发费用
     *
     * @param uscc 统一社会信用代码
     * @return
     */
    @Override
    public AjaxResult queryResearchAndDevelopmentExpenseDetails(String uscc) {

        //查询本企业各年份的开发研费支出
        List<PreMuBenefitEvaluation> result = cockpitMapper.selectCompanyRdFundsByUscc(uscc);
        Map<String,Object> re = new HashMap<>(16);

        if(result != null && !result.isEmpty()){
            //构建折线图数据
            List<String> xAxis = new ArrayList<String>();
            List<String> yAxis = new ArrayList<String>();

            for(PreMuBenefitEvaluation temp : result){
                String years = temp.getYears();
                xAxis.add(years);
                BigDecimal rdFunds = temp.getRdFunds();
                yAxis.add(rdFunds.toString());
            }

            //获取出最新年份的企业大类
            String industryType = result.get(result.size() - 1).getIndustryType();
            String years = result.get(result.size() -1).getYears();

            if(StringUtils.isNotBlank(industryType)){
                BigDecimal rdFunds = result.get(result.size() -1 ).getRdFunds() == null ? BigDecimal.ZERO : result.get(result.size() -1 ).getRdFunds();
                //查询该年度该行业研发费的平均值
                BigDecimal industryRdFunsAvg = cockpitMapper.selectCurrentYearIndustryTypeAvg(industryType,years);
                if(rdFunds.compareTo(industryRdFunsAvg) == -1){
                    re.put("msg","单位用地研发费支出低于同行业内的平均水平");
                }else if(rdFunds.compareTo(industryRdFunsAvg) == 1){
                    re.put("msg","单位用地研发费支出高于同行业内的平均水平");
                }else{
                    re.put("msg","单位用地研发费支出等于同行业内的平均水平");
                }
            }else{
                re.put("msg","");
            }
            //获取出最新一年的研发费数据
            BigDecimal rdFunds = result.get(result.size() -1 ).getRdFunds();
            if(rdFunds == null){
                re.put("newFdFunds","");
            }else{
                re.put("newFdFunds",rdFunds.toString());
            }

            re.put("x",xAxis);
            re.put("y",yAxis);
        }else{
            re.put("x",new ArrayList<String>());
            re.put("y",new ArrayList<String>());
            re.put("msg","");
            re.put("newFdFunds","");
        }
        return AjaxResult.success(re);
    }

    /**
     * 查询企业员工人数
     *
     * @param uscc
     * @param year
     * @return
     */
    @Override
    public AjaxResult queryNumberEmployees(String uscc, String year) {
        Integer yearInteger = Integer.valueOf(year);
        Integer preYear = yearInteger - 4;

        Map<String,Object> re = new HashMap<>(16);

        List<PreMuBenefitEvaluation> result = cockpitMapper.queryNumberEmployeesByUscc(uscc,yearInteger,preYear);
        if(result != null && !result.isEmpty()){

            List<String> xAxis = new ArrayList<>();
            List<String> yAxis = new ArrayList<>();

            for(PreMuBenefitEvaluation temp : result){
                String years = temp.getYears();
                xAxis.add(years);
                Long annualEmployees = temp.getAnnualEmployees();
                yAxis.add(String.valueOf(annualEmployees));
            }

            //设置描述
            //该年度同行业人员
            String industryType = result.get(result.size() -1 ).getIndustryType();
            Long annualEmployees = result.get(result.size() -1).getAnnualEmployees();
            if(StringUtils.isNotBlank(industryType)){
                BigDecimal companyPopelaAvg = cockpitMapper.selectCompanyPopelaYearAndIndustryType(industryType,year);
                companyPopelaAvg = companyPopelaAvg == null ? BigDecimal.ZERO : companyPopelaAvg;

                BigDecimal annualEmployessBig = BigDecimal.valueOf(annualEmployees == null ? 0L : annualEmployees);
                if(annualEmployessBig.compareTo(companyPopelaAvg) == -1){
                    re.put("msg","市场员工人数低于同行业内的平均水平");
                } else if (annualEmployessBig.compareTo(companyPopelaAvg) == 1) {
                    re.put("msg","市场员工人数高于同行业内的平均水平");
                }else{
                    re.put("msg","市场员工人数等于同行业内的平均水平");
                }
            }else{
                re.put("msg","");
            }
            re.put("newAnnual",annualEmployees);
            re.put("x",xAxis);
            re.put("y",yAxis);
        }else{
            re.put("x",new ArrayList<>());
            re.put("y",new ArrayList<>());
            re.put("msg","");
            re.put("newAnnual","");
        }
        return AjaxResult.success(re);
    }

    /**
     * 查询单位数据
     *
     * @param uscc
     * @return
     */
    @Override
    public AjaxResult queryDwData(String uscc) {
        List<PreMuBenefitEvaluation> result = cockpitMapper.queryDwData(uscc);

        Map<String,Object> resultMap = new HashMap<>(16);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> actualTaxList = new ArrayList<>();
        List<BigDecimal> mainIncomeList = new ArrayList<>();
        List<String> industryTypeList = new ArrayList<>();

        if(result != null && !result.isEmpty()){
            for(PreMuBenefitEvaluation temp : result){
                String years = temp.getYears();
                xAxis.add(years);
                BigDecimal actualTax = temp.getActualTax();
                actualTaxList.add(actualTax);
                BigDecimal mainIncome = temp.getMainIncome();
                mainIncomeList.add(mainIncome);
                String s = temp.getpValue();
                industryTypeList.add(s);
            }
            resultMap.put("x",xAxis);
            resultMap.put("actualTaxList",actualTaxList);
            resultMap.put("mainIncomeList",mainIncomeList);
            resultMap.put("industryTypeList",industryTypeList);
        }else{
            resultMap.put("x",new ArrayList<>());
            resultMap.put("actualTaxList",new ArrayList<>());
            resultMap.put("mainIncomeList",new ArrayList<>());
            resultMap.put("industryTypeList",new ArrayList<>());
        }
        return AjaxResult.success(resultMap);
    }

    /**
     * 查询明细
     *
     * @param type
     * @return
     */
    @Override
    public AjaxResult queryDetail(String type,String uscc) {

        //增值税
        if(StringUtils.equals(type,"zzs")){
            List<SsDetailDto> ssDetailDtos = queryZzsDetail(uscc);

            return AjaxResult.success(ssDetailDtos);
        }else if(StringUtils.equals(type,"xfs")){
            return AjaxResult.success(queryXfsDetail(uscc));
        } else if (StringUtils.equals(type, "qysds")) {
            return AjaxResult.success(queryQysdsDetail(uscc));
        } else if (StringUtils.equals(type, "grsds")) {
            return AjaxResult.success(queryGrsdsDetail(uscc));
        } else if (StringUtils.equals(type, "zys")) {
            return AjaxResult.success(queryZysDetail(uscc));
        } else if (StringUtils.equals(type, "cswhjss")) {
            return AjaxResult.success(queryCswhjssDetail(uscc));
        } else if (StringUtils.equals(type, "fcs")) {
            return AjaxResult.success(queryFcsDetail(uscc));
        } else if (StringUtils.equals(type, "yhs")) {
            return AjaxResult.success(queryYhsDetail(uscc));
        } else if (StringUtils.equals(type, "cztdsys")) {
            return AjaxResult.success(queryCztdsysDetail(uscc));
        }else if (StringUtils.equals(type, "tdzzs")) {
            return AjaxResult.success(queryTdzzsDetail(uscc));
        }else if (StringUtils.equals(type, "ccs")) {
            return AjaxResult.success(queryCcsDetail(uscc));
        }else if (StringUtils.equals(type, "clgzs")) {
            return AjaxResult.success(queryClgzsDetail(uscc));
        }else if (StringUtils.equals(type, "gdzys")) {
            return AjaxResult.success(queryGdzysDetail(uscc));
        }else if (StringUtils.equals(type, "qs")) {
            return AjaxResult.success(queryQsDetail(uscc));
        }else if (StringUtils.equals(type, "hjbhs")) {
            return AjaxResult.success(queryHjbhsDetail(uscc));
        }else if (StringUtils.equals(type, "yss")) {
            return AjaxResult.success(queryYssDetail(uscc));
        }

        return AjaxResult.warn("没有找到正确的税收类型");
    }

    @Override
    public List<String> queryCompanyYears(String uscc) {
        List<String> result = cockpitMapper.queryCompanyYears(uscc);
        return result;
    }

    /**
     * 查询企来基本信息
     *
     * @param uscc
     * @return
     */
    @Override
    public AjaxResult queryCompanyBaseInfo(String uscc) {
        Map map = cockpitMapper.queryCompanyBaseInfo(uscc);
        return AjaxResult.success(map);
    }

    private List<SsDetailDto> queryYssDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryYssDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryHjbhsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryHjbhsDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryQsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryQsDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryGdzysDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryGdzysDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryClgzsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryClgzsDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryCcsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryCcsDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryTdzzsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryTdzzsDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryCztdsysDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryCztdsysDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryYhsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryYhsDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryFcsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryFcsDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryCswhjssDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryCswhjssDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryZysDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryZysDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryGrsdsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryGrsdsDetail(uscc);
        return result;
    }

    private List<SsDetailDto> queryQysdsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryQysdsDetail(uscc);
        return result;
    }

    /**
     * 查询消费税
     * @param uscc 社会信用统一代码
     * @return
     */
    private List<SsDetailDto> queryXfsDetail(String uscc) {
        List<SsDetailDto> result = cockpitMapper.queryXfsDetail(uscc);
        return result;
    }

    /**
     * 查询增值税
     * @param uscc 社会信用统一代码
     * @return
     */
    private List<SsDetailDto> queryZzsDetail(String uscc){
        List<SsDetailDto> result = cockpitMapper.queryZzsYear(uscc);
        return result;
    }

    /**
     * 查询企业年度耗煤明细
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndAnnualCoalConsumptionDetails(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        //查询单位能耗销售收入
        BigDecimal dwnhxssr = cockpitMapper.selectDwNhXssr(uscc, years);
        if(dwnhxssr == null){
            dwnhxssr = BigDecimal.ZERO;
        }
        map.put("dwnhxssr",dwnhxssr);
        /** 耗煤量总量 */
        BigDecimal coalConsumption = cockpitMapper.selectCoalConsumptionByUsccAndYears(uscc, years);
        if (coalConsumption != null) {
            coalConsumption = coalConsumption.setScale(2, RoundingMode.HALF_UP);
        }else{
            coalConsumption = BigDecimal.ZERO;
        }

        /** 单位用地耗煤量 */
        BigDecimal coalConsumptionPerUnitOfLand = cockpitMapper.selectCoalConsumptionPerUnitOfLandByUsccAndYears(uscc, years);
        if (coalConsumptionPerUnitOfLand != null) {
            coalConsumptionPerUnitOfLand = coalConsumptionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            coalConsumptionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 行业单位用地耗煤量 */
        String industryType = cockpitMapper.selectIndustryTypeByUsccAndYears(uscc, years);//获取该企业所属的行业类型
        BigDecimal industryCoalConsumptionPerUnitOfLand = cockpitMapper.selectIndustryCoalConsumptionPerUnitOfLandByUsccAndYears(uscc, years, industryType);
        if (industryCoalConsumptionPerUnitOfLand != null) {
            industryCoalConsumptionPerUnitOfLand = industryCoalConsumptionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            industryCoalConsumptionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 耗煤量与同行业平均水平的对比 */
        String coalConsumptionCompare = null;
        BigDecimal industryCoalConsumptionPerUnitOfLandAvg = cockpitMapper.selectIndustryCoalConsumptionPerUnitOfLandByUsccAndYearsAvg(uscc, years, industryType);
        if (coalConsumptionPerUnitOfLand != null && industryCoalConsumptionPerUnitOfLandAvg != null) {
            int result = coalConsumption.compareTo(industryCoalConsumptionPerUnitOfLandAvg);
            if (result > 0) {
                coalConsumptionCompare = "耗煤量高于同行业平均水平";
            } else if (result == 0) {
                coalConsumptionCompare = "耗煤量等同于同行业平均水平";
            } else {
                coalConsumptionCompare = "耗煤量低于同行业平均水平";
            }
        }

        /** 行业单位用地耗煤量占比 */
        BigDecimal industryCoalConsumptionPerUnitOfLandProportion = null;
        if (coalConsumptionPerUnitOfLand != null && industryCoalConsumptionPerUnitOfLand != null) {
            if (industryCoalConsumptionPerUnitOfLand.compareTo(BigDecimal.ZERO) != 0) {
                try {
                    industryCoalConsumptionPerUnitOfLandProportion = coalConsumptionPerUnitOfLand.divide(industryCoalConsumptionPerUnitOfLand, 2, RoundingMode.HALF_UP);
                } catch (ArithmeticException e) {
                    industryCoalConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
                }
            }else{
                industryCoalConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
            }
        }else{
            industryCoalConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
        }

        map.put("zl", coalConsumption); //总量
        map.put("dw", coalConsumptionPerUnitOfLand); //单位
        map.put("hy", industryCoalConsumptionPerUnitOfLand); //行业
        map.put("xx", coalConsumptionCompare); //消息
        map.put("zb", industryCoalConsumptionPerUnitOfLandProportion);  //占比

        return map;
    }

    /**
     * 查询企业年度耗电明细
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndAnnualPowerConsumptionDetails(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        //查询单位能耗销售收入
        BigDecimal dwnhxssr = cockpitMapper.selectDwNhXssr(uscc, years);
        if(dwnhxssr == null){
            dwnhxssr = BigDecimal.ZERO;
        }
        map.put("dwnhxssr",dwnhxssr);
        /** 耗电量总量 */
        BigDecimal powerConsumption = cockpitMapper.selectPowerConsumptionByUsccAndYears(uscc, years);
        if (powerConsumption != null) {
            powerConsumption = powerConsumption.setScale(2, RoundingMode.HALF_UP);
        }else{
            powerConsumption = BigDecimal.ZERO;
        }

        /** 单位用地耗电量 */
        BigDecimal powerConsumptionPerUnitOfLand = cockpitMapper.selectPowerConsumptionPerUnitOfLandByUsccAndYears(uscc, years);
        if (powerConsumptionPerUnitOfLand != null) {
            powerConsumptionPerUnitOfLand = powerConsumptionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            powerConsumptionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 行业单位用地耗电量 */
        String industryType = cockpitMapper.selectIndustryTypeByUsccAndYears(uscc, years);
        BigDecimal industryPowerConsumptionPerUnitOfLand = cockpitMapper.selectIndustryPowerConsumptionPerUnitOfLandByUsccAndYears(uscc, years, industryType);
        if (industryPowerConsumptionPerUnitOfLand != null) {
            industryPowerConsumptionPerUnitOfLand = industryPowerConsumptionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            industryPowerConsumptionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 耗电量与同行业平均水平的对比 */
        String powerConsumptionCompare = null;
        BigDecimal industryPowerConsumptionPerUnitOfLandAvg = cockpitMapper.selectIndustryPowerConsumptionPerUnitOfLandByUsccAndYearsAvg(uscc, years, industryType);
        if (powerConsumptionPerUnitOfLand != null && industryPowerConsumptionPerUnitOfLandAvg != null) {
            int result = powerConsumptionPerUnitOfLand.compareTo(industryPowerConsumptionPerUnitOfLandAvg);
            if (result > 0) {
                powerConsumptionCompare = "耗电量高于同行业平均水平";
            } else if (result == 0) {
                powerConsumptionCompare = "耗电量等同于同行业平均水平";
            } else {
                powerConsumptionCompare = "耗电量低于同行业平均水平";
            }
        }

        /** 行业单位用地耗电量占比 */
        BigDecimal industryPowerConsumptionPerUnitOfLandProportion = null;
        if (powerConsumptionPerUnitOfLand != null && industryPowerConsumptionPerUnitOfLand != null) {
            if (industryPowerConsumptionPerUnitOfLand.compareTo(BigDecimal.ZERO) != 0) {
                try {
                    industryPowerConsumptionPerUnitOfLandProportion = powerConsumptionPerUnitOfLand.divide(industryPowerConsumptionPerUnitOfLand, 2, RoundingMode.HALF_UP);
                } catch (ArithmeticException e) {
                    industryPowerConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
                }
            }else{
                industryPowerConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
            }
        }else{
            industryPowerConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
        }

        map.put("zl", powerConsumption);
        map.put("dw", powerConsumptionPerUnitOfLand);
        map.put("hy", industryPowerConsumptionPerUnitOfLand);
        map.put("xx", powerConsumptionCompare);
        map.put("zb", industryPowerConsumptionPerUnitOfLandProportion);

        return map;
    }

    /**
     * 查询企业年度耗气明细
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndAnnualGasConsumptionDetails(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        //查询单位能耗销售收入
        BigDecimal dwnhxssr = cockpitMapper.selectDwNhXssr(uscc, years);
        if(dwnhxssr == null){
            dwnhxssr = BigDecimal.ZERO;
        }
        map.put("dwnhxssr",dwnhxssr);
        /** 耗气量总量 */
        BigDecimal gasConsumption = cockpitMapper.selectGasConsumptionByUsccAndYears(uscc, years);
        if (gasConsumption != null) {
            gasConsumption = gasConsumption.setScale(2, RoundingMode.HALF_UP);
        }else{
            gasConsumption = BigDecimal.ZERO;
        }

        /** 单位用地耗气量 */
        BigDecimal gasConsumptionPerUnitOfLand = cockpitMapper.selectGasConsumptionPerUnitOfLandByUsccAndYears(uscc, years);
        if (gasConsumptionPerUnitOfLand != null) {
            gasConsumptionPerUnitOfLand = gasConsumptionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            gasConsumptionPerUnitOfLand = BigDecimal.ZERO;
        }
        /** 行业单位用地耗气量 */
        String industryType = cockpitMapper.selectIndustryTypeByUsccAndYears(uscc, years);
        BigDecimal industryGasConsumptionPerUnitOfLand = cockpitMapper.selectIndustryGasConsumptionPerUnitOfLandByUsccAndYears(uscc, years, industryType);
        if (industryGasConsumptionPerUnitOfLand != null) {
            industryGasConsumptionPerUnitOfLand = industryGasConsumptionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            industryGasConsumptionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 耗气量与同行业平均水平的对比 */
        String gasConsumptionCompare = null;
        BigDecimal industryGasConsumptionPerUnitOfLandAvg = cockpitMapper.selectIndustryGasConsumptionPerUnitOfLandByUsccAndYearsAvg(uscc, years, industryType);
        if (gasConsumptionPerUnitOfLand != null && industryGasConsumptionPerUnitOfLandAvg != null) {
            int result = gasConsumptionPerUnitOfLand.compareTo(industryGasConsumptionPerUnitOfLandAvg);
            if (result > 0) {
                gasConsumptionCompare = "耗气量高于同行业平均水平";
            } else if (result == 0) {
                gasConsumptionCompare = "耗气量等同于同行业平均水平";
            } else {
                gasConsumptionCompare = "耗气量低于同行业平均水平";
            }
        }

        /** 行业单位用地耗气量占比 */
        BigDecimal industryGasConsumptionPerUnitOfLandProportion = null;
        if (gasConsumptionPerUnitOfLand != null && industryGasConsumptionPerUnitOfLand != null) {
            if (industryGasConsumptionPerUnitOfLand.compareTo(BigDecimal.ZERO) != 0) {
                try {
                    industryGasConsumptionPerUnitOfLandProportion = gasConsumptionPerUnitOfLand.divide(industryGasConsumptionPerUnitOfLand, 2, RoundingMode.HALF_UP);
                } catch (ArithmeticException e) {
                    industryGasConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
                }
            }else{
                industryGasConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
            }
        }else{
            industryGasConsumptionPerUnitOfLandProportion = BigDecimal.ZERO;
        }

        map.put("zl", gasConsumption);
        map.put("dw", gasConsumptionPerUnitOfLand);
        map.put("hy", industryGasConsumptionPerUnitOfLand);
        map.put("xx", gasConsumptionCompare);
        map.put("zb", industryGasConsumptionPerUnitOfLandProportion);

        return map;
    }

    /**
     * 查询企业年度化学需氧量排放明细
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndAnnualO2EmissionDetails(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        //查询单位污染物排放销售收入
        BigDecimal dwWrwXssr = cockpitMapper.selectDwWrwXssr(uscc,years);
        if(dwWrwXssr == null){
            dwWrwXssr = BigDecimal.ZERO;
        }
        map.put("dwwrwxssr",dwWrwXssr);
        /** 化学需氧量排放量总量 */
        BigDecimal o2Emission = cockpitMapper.selectO2EmissionByUsccAndYears(uscc, years);
        if (o2Emission != null) {
            o2Emission = o2Emission.setScale(2, RoundingMode.HALF_UP);
        }else{
            o2Emission = BigDecimal.ZERO;
        }

        /** 单位用地化学需氧量排放 */
        BigDecimal o2EmissionPerUnitOfLand = cockpitMapper.selectO2EmissionPerUnitOfLandByUsccAndYears(uscc, years);
        if (o2EmissionPerUnitOfLand != null) {
            o2EmissionPerUnitOfLand = o2EmissionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            o2EmissionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 行业单位用地化学需氧量排放 */
        String industryType = cockpitMapper.selectIndustryTypeByUsccAndYears(uscc, years);
        BigDecimal industryO2EmissionPerUnitOfLand = cockpitMapper.selectIndustryO2EmissionPerUnitOfLandByUsccAndYears(uscc, years, industryType);
        if (industryO2EmissionPerUnitOfLand != null) {
            industryO2EmissionPerUnitOfLand = industryO2EmissionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            industryO2EmissionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 化学需氧量的排放与同行业平均水平的对比 */
        String o2EmissionCompare = null;
        BigDecimal industryO2EmissionPerUnitOfLandAvg = cockpitMapper.selectIndustryO2EmissionPerUnitOfLandByUsccAndYearsAvg(uscc, years, industryType);
        if (o2EmissionPerUnitOfLand != null && industryO2EmissionPerUnitOfLandAvg != null) {
            int result = o2EmissionPerUnitOfLand.compareTo(industryO2EmissionPerUnitOfLandAvg);
            if (result > 0) {
                o2EmissionCompare = "化学需氧量排放高于同行业平均水平";
            } else if (result == 0) {
                o2EmissionCompare = "化学需氧量排放等同于同行业平均水平";
            } else {
                o2EmissionCompare = "化学需氧量排放低于同行业平均水平";
            }
        }

        /** 行业单位用地化学需氧量排放占比 */
        BigDecimal industryO2EmissionPerUnitOfLandProportion = null;
        if (o2EmissionPerUnitOfLand != null && industryO2EmissionPerUnitOfLand != null) {
            if (industryO2EmissionPerUnitOfLand.compareTo(BigDecimal.ZERO) != 0) {
                try {
                    industryO2EmissionPerUnitOfLandProportion = o2EmissionPerUnitOfLand.divide(industryO2EmissionPerUnitOfLand, 2, RoundingMode.HALF_UP);
                } catch (ArithmeticException e) {
                    //----------------异常处理(若不加额外处理则返回null)----------------
                    industryO2EmissionPerUnitOfLandProportion = BigDecimal.ZERO;
                }
            }else{
                industryO2EmissionPerUnitOfLandProportion = BigDecimal.ZERO;
            }
        }else{
            industryO2EmissionPerUnitOfLandProportion = BigDecimal.ZERO;
        }

        map.put("zl", o2Emission);
        map.put("dw", o2EmissionPerUnitOfLand);
        map.put("hy", industryO2EmissionPerUnitOfLand);
        map.put("xx", o2EmissionCompare);
        map.put("zb", industryO2EmissionPerUnitOfLandProportion);

        return map;
    }

    /**
     * 查询企业年度氨氮排放明细
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndAnnualAmmoniaEmissionDetails(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        //查询单位污染物排放销售收入
        BigDecimal dwWrwXssr = cockpitMapper.selectDwWrwXssr(uscc,years);
        if(dwWrwXssr == null){
            dwWrwXssr = BigDecimal.ZERO;
        }
        map.put("dwwrwxssr",dwWrwXssr);
        /** 氨氮排放量总量 */
        BigDecimal ammoniaEmission = cockpitMapper.selectAmmoniaEmissionByUsccAndYears(uscc, years);
        if (ammoniaEmission != null) {
            ammoniaEmission = ammoniaEmission.setScale(2, RoundingMode.HALF_UP);
        }else{
            ammoniaEmission = BigDecimal.ZERO;
        }

        /** 单位用地氨氮排放 */
        BigDecimal ammoniaEmissionPerUnitOfLand = cockpitMapper.selectAmmoniaEmissionPerUnitOfLandByUsccAndYears(uscc, years);
        if (ammoniaEmissionPerUnitOfLand != null) {
            ammoniaEmissionPerUnitOfLand = ammoniaEmissionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            ammoniaEmissionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 行业单位用地氨氮排放 */
        String industryType = cockpitMapper.selectIndustryTypeByUsccAndYears(uscc, years);
        BigDecimal industryAmmoniaEmissionPerUnitOfLand = cockpitMapper.selectIndustryAmmoniaEmissionPerUnitOfLandByUsccAndYears(uscc, years, industryType);
        if (industryAmmoniaEmissionPerUnitOfLand != null) {
            industryAmmoniaEmissionPerUnitOfLand = industryAmmoniaEmissionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            industryAmmoniaEmissionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 氨氮的排放与同行业平均水平的对比 */
        String ammoniaEmissionCompare = null;
        BigDecimal industryAmmoniaEmissionPerUnitOfLandAvg = cockpitMapper.selectIndustryAmmoniaEmissionPerUnitOfLandByUsccAndYearsAvg(uscc, years, industryType);
        if (ammoniaEmissionPerUnitOfLand != null && industryAmmoniaEmissionPerUnitOfLandAvg != null) {
            int result = ammoniaEmissionPerUnitOfLand.compareTo(industryAmmoniaEmissionPerUnitOfLandAvg);
            if (result > 0) {
                ammoniaEmissionCompare = "氨氮排放高于同行业平均水平";
            } else if (result == 0) {
                ammoniaEmissionCompare = "氨氮排放等同于同行业平均水平";
            } else {
                ammoniaEmissionCompare = "氨氮排放低于同行业平均水平";
            }
        }

        /** 行业单位用地氨氮排放占比 */
        BigDecimal industryAmmoniaEmissionPerUnitOfLandProportion = null;
        if (ammoniaEmissionPerUnitOfLand != null && industryAmmoniaEmissionPerUnitOfLand != null) {
            if (industryAmmoniaEmissionPerUnitOfLand.compareTo(BigDecimal.ZERO) != 0) {
                try {
                    industryAmmoniaEmissionPerUnitOfLandProportion = ammoniaEmissionPerUnitOfLand.divide(industryAmmoniaEmissionPerUnitOfLand, 2, RoundingMode.HALF_UP);
                } catch (ArithmeticException e) {
                    industryAmmoniaEmissionPerUnitOfLandProportion = BigDecimal.ZERO;
                }
            }else{
                industryAmmoniaEmissionPerUnitOfLandProportion = BigDecimal.ZERO;
            }
        }else{
            industryAmmoniaEmissionPerUnitOfLandProportion = BigDecimal.ZERO;
        }

        map.put("zl", ammoniaEmission);
        map.put("dw", ammoniaEmissionPerUnitOfLand);
        map.put("hy", industryAmmoniaEmissionPerUnitOfLand);
        map.put("xx", ammoniaEmissionCompare);
        map.put("zb", industryAmmoniaEmissionPerUnitOfLandProportion);

        return map;
    }

    /**
     * 查询企业年度二氧化硫排放明细
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndAnnualSo2EmissionDetails(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        //查询单位污染物排放销售收入
        BigDecimal dwWrwXssr = cockpitMapper.selectDwWrwXssr(uscc,years);
        if(dwWrwXssr == null){
            dwWrwXssr = BigDecimal.ZERO;
        }
        map.put("dwwrwxssr",dwWrwXssr);
        /** 二氧化硫排放量总量 */
        BigDecimal so2Emission = cockpitMapper.selectSo2EmissionByUsccAndYears(uscc, years);
        if (so2Emission != null) {
            so2Emission = so2Emission.setScale(2, RoundingMode.HALF_UP);
        }else{
            so2Emission = BigDecimal.ZERO;
        }

        /** 单位用地二氧化硫排放 */
        BigDecimal so2EmissionPerUnitOfLand = cockpitMapper.selectSo2EmissionPerUnitOfLandByUsccAndYears(uscc, years);
        if (so2EmissionPerUnitOfLand != null) {
            so2EmissionPerUnitOfLand = so2EmissionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            so2EmissionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 行业单位用地二氧化硫排放 */
        String industryType = cockpitMapper.selectIndustryTypeByUsccAndYears(uscc, years);
        BigDecimal industrySo2EmissionPerUnitOfLand = cockpitMapper.selectIndustrySo2EmissionPerUnitOfLandByUsccAndYears(uscc, years, industryType);
        if (industrySo2EmissionPerUnitOfLand != null) {
            industrySo2EmissionPerUnitOfLand = industrySo2EmissionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            industrySo2EmissionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 二氧化硫的排放与同行业平均水平的对比 */
        String so2EmissionCompare = null;
        BigDecimal industrySo2EmissionPerUnitOfLandAvg = cockpitMapper.selectIndustrySo2EmissionPerUnitOfLandByUsccAndYearsAvg(uscc, years, industryType);
        if (so2EmissionPerUnitOfLand != null && industrySo2EmissionPerUnitOfLandAvg != null) {
            int result = so2EmissionPerUnitOfLand.compareTo(industrySo2EmissionPerUnitOfLandAvg);
            if (result > 0) {
                so2EmissionCompare = "二氧化硫排放高于同行业平均水平";
            } else if (result == 0) {
                so2EmissionCompare = "二氧化硫排放等同于同行业平均水平";
            } else {
                so2EmissionCompare = "二氧化硫排放低于同行业平均水平";
            }
        }

        /** 行业单位用地二氧化硫排放占比 */
        BigDecimal industrySo2EmissionPerUnitOfLandProportion = null;
        if (so2EmissionPerUnitOfLand != null && industrySo2EmissionPerUnitOfLand != null) {
            if (industrySo2EmissionPerUnitOfLand.compareTo(BigDecimal.ZERO) != 0) {
                try {
                    industrySo2EmissionPerUnitOfLandProportion = so2EmissionPerUnitOfLand.divide(industrySo2EmissionPerUnitOfLand, 2, RoundingMode.HALF_UP);
                } catch (ArithmeticException e) {
                    industrySo2EmissionPerUnitOfLandProportion = BigDecimal.ZERO;
                }
            }else{
                industrySo2EmissionPerUnitOfLandProportion = BigDecimal.ZERO;
            }
        }else{
            industrySo2EmissionPerUnitOfLandProportion = BigDecimal.ZERO;
        }

        map.put("zl", so2Emission);
        map.put("dw", so2EmissionPerUnitOfLand);
        map.put("hy", industrySo2EmissionPerUnitOfLand);
        map.put("xx", so2EmissionCompare);
        map.put("zb", industrySo2EmissionPerUnitOfLandProportion);

        return map;
    }

    /**
     * 查询企业年度氮氧化物排放明细
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndAnnualNoxEmissionDetails(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        //查询单位污染物排放销售收入
        BigDecimal dwWrwXssr = cockpitMapper.selectDwWrwXssr(uscc,years);
        if(dwWrwXssr == null){
            dwWrwXssr = BigDecimal.ZERO;
        }
        map.put("dwwrwxssr",dwWrwXssr);
        /** 氮氧化物排放量总量 */
        BigDecimal noxEmission = cockpitMapper.selectNoxEmissionByUsccAndYears(uscc, years);
        if (noxEmission != null) {
            noxEmission = noxEmission.setScale(2, RoundingMode.HALF_UP);
        }else{
            noxEmission = BigDecimal.ZERO;
        }

        /** 单位用地氮氧化物排放 */
        BigDecimal noxEmissionPerUnitOfLand = cockpitMapper.selectNoxEmissionPerUnitOfLandByUsccAndYears(uscc, years);
        if (noxEmissionPerUnitOfLand != null) {
            noxEmissionPerUnitOfLand = noxEmissionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            noxEmissionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 行业单位用地氮氧化物排放 */
        String industryType = cockpitMapper.selectIndustryTypeByUsccAndYears(uscc, years);
        BigDecimal industryNoxEmissionPerUnitOfLand = cockpitMapper.selectIndustryNoxEmissionPerUnitOfLandByUsccAndYears(uscc, years, industryType);
        if (industryNoxEmissionPerUnitOfLand != null) {
            industryNoxEmissionPerUnitOfLand = industryNoxEmissionPerUnitOfLand.setScale(2, RoundingMode.HALF_UP);
        }else{
            industryNoxEmissionPerUnitOfLand = BigDecimal.ZERO;
        }

        /** 氮氧化物的排放与同行业平均水平的对比 */
        String noxEmissionCompare = null;
        BigDecimal industryNoxEmissionPerUnitOfLandAvg = cockpitMapper.selectIndustryNoxEmissionPerUnitOfLandByUsccAndYearsAvg(uscc, years, industryType);
        if (noxEmissionPerUnitOfLand != null && industryNoxEmissionPerUnitOfLandAvg != null) {
            int result = noxEmissionPerUnitOfLand.compareTo(industryNoxEmissionPerUnitOfLandAvg);
            if (result > 0) {
                noxEmissionCompare = "氮氧化物排放高于同行业平均水平";
            } else if (result == 0) {
                noxEmissionCompare = "氮氧化物排放等同于同行业平均水平";
            } else {
                noxEmissionCompare = "氮氧化物排放低于同行业平均水平";
            }
        }

        /** 行业单位用地氮氧化物排放占比 */
        BigDecimal industryNoxEmissionPerUnitOfLandProportion = null;
        if (noxEmissionPerUnitOfLand != null && industryNoxEmissionPerUnitOfLand != null) {
            if (industryNoxEmissionPerUnitOfLand.compareTo(BigDecimal.ZERO) != 0) {
                try {
                    industryNoxEmissionPerUnitOfLandProportion = noxEmissionPerUnitOfLand.divide(industryNoxEmissionPerUnitOfLand, 2, RoundingMode.HALF_UP);
                } catch (ArithmeticException e) {
                    industryNoxEmissionPerUnitOfLandProportion = BigDecimal.ZERO;
                }
            }else{
                industryNoxEmissionPerUnitOfLandProportion = BigDecimal.ZERO;
            }
        }else{
            industryNoxEmissionPerUnitOfLandProportion = BigDecimal.ZERO;
        }

        map.put("zl", noxEmission);
        map.put("dw", noxEmissionPerUnitOfLand);
        map.put("hy", industryNoxEmissionPerUnitOfLand);
        map.put("xx", noxEmissionCompare);
        map.put("zb", industryNoxEmissionPerUnitOfLandProportion);

        return map;
    }

    /**
     * 查询企业自有土地使用权土地占比
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndUseAreaProportion(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        List<Map<String, Object>> maps = cockpitMapper.selectUseAreaAndSumAreaAndProportionByUsccAndYears(uscc, years);
        if(maps != null && !maps.isEmpty()){
            Map<String, Object> resultMap = maps.get(0);
            BigDecimal useArea = (BigDecimal) resultMap.get("usearea");
            if (useArea != null) {
                useArea = useArea.setScale(2, RoundingMode.HALF_UP);
            }else{
                useArea = BigDecimal.ZERO;
            }
            BigDecimal sumArea = (BigDecimal) resultMap.get("sumarea");
            if (sumArea != null) {
                sumArea = sumArea.setScale(2, RoundingMode.HALF_UP);
            }else{
                sumArea = BigDecimal.ZERO;
            }

            BigDecimal useAreaProportion = (BigDecimal) resultMap.get("useareaproportion");
            if (useAreaProportion != null) {
                useAreaProportion = useAreaProportion.setScale(2, RoundingMode.HALF_UP);
            }else{
                useAreaProportion = BigDecimal.ZERO;
            }

            map.put("value", useArea);
            map.put("sumArea", sumArea);
            map.put("zb", useAreaProportion);
        }else{
            map.put("value", BigDecimal.ZERO);
            map.put("sumArea", BigDecimal.ZERO);
            map.put("zb", BigDecimal.ZERO);
        }
        return map;
    }

    /**
     * 查询企业通过租赁方式实际占用土地占比
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndLeasedAreaProportion(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        List<Map<String, Object>> maps = cockpitMapper.selectLeasedAreaAndSumAreaAndProportionByUsccAndYears(uscc, years);
        if(maps != null && !maps.isEmpty()){
            Map<String, Object> resultMap = maps.get(0);
            BigDecimal leasedArea = (BigDecimal) resultMap.get("leasedarea");
            if (leasedArea != null) {
                leasedArea = leasedArea.setScale(2, RoundingMode.HALF_UP);
            }else{
                leasedArea = BigDecimal.ZERO;
            }
            BigDecimal sumArea = (BigDecimal) resultMap.get("sumarea");
            if (sumArea != null) {
                sumArea = sumArea.setScale(2, RoundingMode.HALF_UP);
            }else{
                sumArea = BigDecimal.ZERO;
            }
            BigDecimal leasedAreaProportion = (BigDecimal) resultMap.get("leasedareaproportion");
            if (leasedAreaProportion != null) {
                leasedAreaProportion = leasedAreaProportion.setScale(2, RoundingMode.HALF_UP);
            }else{
                leasedAreaProportion = BigDecimal.ZERO;
            }
            map.put("value", leasedArea);
            map.put("sumArea", sumArea);
            map.put("zb", leasedAreaProportion);
        }else{
            map.put("value", BigDecimal.ZERO);
            map.put("sumArea", BigDecimal.ZERO);
            map.put("zb", BigDecimal.ZERO);
        }
        return map;
    }

    /**
     * 查询企业其他实际占用土地占比
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndOtherAreaProportion(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        List<Map<String, Object>> maps = cockpitMapper.selectOtherAreaAndSumAreaAndProportionByUsccAndYears(uscc, years);
        if(maps != null && !maps.isEmpty()){
            Map<String, Object> resultMap = maps.get(0);
            BigDecimal otherArea = (BigDecimal) resultMap.get("otherarea");
            if (otherArea != null) {
                otherArea = otherArea.setScale(2, RoundingMode.HALF_UP);
            }else{
                otherArea = BigDecimal.ZERO;
            }
            BigDecimal sumArea = (BigDecimal) resultMap.get("sumarea");
            if (sumArea != null) {
                sumArea = sumArea.setScale(2, RoundingMode.HALF_UP);
            }else{
                sumArea = BigDecimal.ZERO;
            }
            BigDecimal otherAreaProportion = (BigDecimal) resultMap.get("otherareaproportion");
            if (otherAreaProportion != null) {
                otherAreaProportion = otherAreaProportion.setScale(2, RoundingMode.HALF_UP);
            }else{
                otherAreaProportion = BigDecimal.ZERO;
            }
            map.put("value", otherArea);
            map.put("sumArea", sumArea);
            map.put("zb", otherAreaProportion);
        }else{
            map.put("value", BigDecimal.ZERO);
            map.put("sumArea", BigDecimal.ZERO);
            map.put("zb", BigDecimal.ZERO);
        }
        return map;
    }

    /**
     * 查询企业对外出租土地占比
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @Override
    public Map<String, Object> queryResearchAndRentOutAreaProportion(String uscc, String years) {
        HashMap<String, Object> map = new HashMap<>();

        List<Map<String, Object>> maps = cockpitMapper.selectRentOutAreaAndSumAreaAndProportionByUsccAndYears(uscc, years);
        if(maps != null && !maps.isEmpty()){
            Map<String, Object> resultMap = maps.get(0);
            BigDecimal rentOutArea = (BigDecimal) resultMap.get("rentoutarea");
            if (rentOutArea != null) {
                rentOutArea = rentOutArea.setScale(2, RoundingMode.HALF_UP);
            }else{
                rentOutArea = BigDecimal.ZERO;
            }
            BigDecimal sumArea = (BigDecimal) resultMap.get("sumarea");
            if (sumArea != null) {
                sumArea = sumArea.setScale(2, RoundingMode.HALF_UP);
            }else{
                sumArea = BigDecimal.ZERO;
            }
            BigDecimal rentOutAreaProportion = (BigDecimal) resultMap.get("rentoutareaproportion");
            if (rentOutAreaProportion != null) {
                rentOutAreaProportion = rentOutAreaProportion.setScale(2, RoundingMode.HALF_UP);
            }else{
                rentOutAreaProportion = BigDecimal.ZERO;
            }

            map.put("value", rentOutArea);
            map.put("sumArea", sumArea);
            map.put("zb", rentOutAreaProportion);
        }else{
            map.put("value", BigDecimal.ZERO);
            map.put("sumArea", BigDecimal.ZERO);
            map.put("zb", BigDecimal.ZERO);
        }
        return map;
    }

}
